package io.renren.modules.generator.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.app.enums.CheckOrderStatusEnum;
import io.renren.modules.app.request.checkorder.SubmitCheckOrderItemReq;
import io.renren.modules.app.request.checkorder.SubmitCheckOrderReq;
import io.renren.modules.generator.entity.CheckOrderEntity;
import io.renren.modules.generator.entity.CheckOrderItemEntity;
import io.renren.modules.generator.entity.PositionEntity;
import io.renren.modules.generator.service.CheckOrderItemService;
import io.renren.modules.generator.service.CheckOrderService;
import io.renren.modules.generator.service.PositionService;
import io.renren.modules.generator.service.StockService;
import io.renren.modules.generator.vo.CkOrderListVo;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 入库单
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-13 23:13:40
 */
@RestController
@RequestMapping("generator/checkorder")
public class CheckOrderController {
    @Autowired
    private CheckOrderService checkOrderService;

    @Autowired
    StockService stockService;

    @Autowired
    PositionService positionService;

    @Autowired
    CheckOrderItemService checkOrderItemService;

    @PostMapping("submitCheckOrder")
    @RequiresPermissions("generator:checkorder:submitorder")
    public R submitCheckOrder(@RequestBody SubmitCheckOrderReq req) {

        SysUserEntity user = ShiroUtils.getUserEntity();

        CheckOrderEntity order = new CheckOrderEntity();
        order.setOrderNo(req.getOrderNo());
        order.setStatus(CheckOrderStatusEnum.初始化.getValue());
        order.setCreateTime(new Date());
        order.setCreateUser(user.getUsername());
        if (checkOrderService.insert(order)) {
            List<CheckOrderItemEntity> orderItems = new ArrayList<>();
            List<SubmitCheckOrderItemReq> reqOrderItems = req.getOrderItems();
            reqOrderItems.stream().forEach(x -> {
                CheckOrderItemEntity vo = new CheckOrderItemEntity();
                BeanUtils.copyProperties(x, vo);
                vo.setItemGuid(x.getGuid());
                vo.setBatchNo(x.getBatchNo());
                vo.setQuantity(x.getQuantity());
                vo.setOrderNo(req.getOrderNo());
                vo.setProportion(100.0);
                PositionEntity pos = positionService.selectOne(Condition.create().eq("ps_code", x.getPsCode()));
                vo.setAreaCode(pos == null ? "" : pos.getAreaCode());
                orderItems.add(vo);
            });
            if (orderItems.size() > 0) {
                checkOrderItemService.insertBatch(orderItems);
            }
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:checkorder:list")
    public R list(@RequestParam Map<String, Object> params) {

        if (params.containsKey("key") && StringUtils.isNotBlank(params.get("key").toString())) {
            params.put("orderNo", params.get("key"));
        }
        List<CkOrderListVo> response = new ArrayList<>();
        PageUtils page = checkOrderService.queryPage(params);
        List<CheckOrderEntity> datas = (List<CheckOrderEntity>) page.getList();
        datas.stream().forEach(x -> {
            CkOrderListVo vo = new CkOrderListVo();
            if (x.getFinishTime().getTime() < 100000) {
                x.setFinishTime(null);
            }
            BeanUtils.copyProperties(x, vo);
            response.add(vo);
        });
        page.setList(response);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:checkorder:info")
    public R info(@PathVariable("id") Long id) {
        CheckOrderEntity checkOrder = checkOrderService.selectById(id);

        return R.ok().put("checkOrder", checkOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:checkorder:save")
    public R save(@RequestBody CheckOrderEntity checkOrder) {
        checkOrderService.insert(checkOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:checkorder:update")
    public R update(@RequestBody CheckOrderEntity checkOrder) {
        checkOrderService.updateById(checkOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:checkorder:delete")
    public R delete(@RequestBody Long[] ids) {
        checkOrderService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
